package bll

import "proto/dto"

var InterfaceAgent = NewInterfaceAgent()

type interfaceAgent struct {
}

func NewInterfaceAgent() *interfaceAgent {
	return &interfaceAgent{}
}

// GetShadowAttributes returns the attributes of the shadow.
func (i *interfaceAgent) GetShadowAttributes(deviceName string) (attributes interface{}, err error) {
	return StateStorage.GetMetaTypeShadow(StateTypeReported, deviceName, MetaTypeAttribute)
}

// GetShadowAttributeValue returns the attributes of the shadow.
func (i *interfaceAgent) GetShadowAttributeValue(deviceName, key string) (attributes interface{}, err error) {
	return StateStorage.GetShadowValue(StateTypeReported, deviceName, MetaTypeAttribute, key)
}

// GetShadowStatusValue returns the status of the shadow.
func (i *interfaceAgent) GetShadowStatusValue(deviceName, key string) (attributes interface{}, err error) {
	return StateStorage.GetShadowValue(StateTypeReported, deviceName, MetaTypeStatus, key)
}

// GetShadowStatus returns the status of the shadow.
func (i *interfaceAgent) GetShadowStatus(deviceName string) (status interface{}, err error) {
	return StateStorage.GetMetaTypeShadow(StateTypeReported, deviceName, MetaTypeStatus)
}

// GetShadow returns the shadow.
func (i *interfaceAgent) GetShadow(deviceName string) (interface{}, error) {
	return StateStorage.Get(deviceName)
}

// UpdateShadowAttributes updates the attributes of the shadow.
func (i *interfaceAgent) UpdateShadowAttributes(deviceName string, attributes []*dto.Item) error {
	for j := 0; j < len(attributes); j++ {
		if metaType, ok := updateDataMap[dto.EnUpdateType_ATTRIBUTE]; !ok {
			continue
		} else {
			return StateStorage.Put(StateTypeDesired, deviceName, metaType, attributes[j].Key, attributes[j].Value)
		}
	}
	return nil
}

// UpdateShadowStatus updates the status of the shadow.
func (i *interfaceAgent) UpdateShadowStatus(deviceName string, status []*dto.Item) error {
	for j := 0; j < len(status); j++ {
		if metaType, ok := updateDataMap[dto.EnUpdateType_STATUS]; !ok {
			continue
		} else {
			return StateStorage.Put(StateTypeDesired, deviceName, metaType, status[j].Key, status[j].Value)
		}
	}
	return nil
}
